#1、导入unittest模块
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
#2、编写测试类TestExercise02,继承unittest类中的testcase
class TestExercise02(unittest.TestCase):
    #3、setUp方法进行环境初始化，tearDown方法进行环境撤销
    def setUp(self):
        print('准备测试数据')
        self.driver = webdriver.Firefox()
        self.data1 = {
            "username":"huahua",
            "mobile":"11011011011",
            "wechat":"er"
        }
    def test_exercise01(self):
        #打开浏览器
        print('执行测试用例')
        self.driver.get('file:///E:/%E8%93%AC%E8%8E%B1%E5%B0%8F%E8%AF%BE2024%E6%B5%8B%E8%AF%95/%E7%AC%AC%E5%9B%9B%E9%98%B6%E6%AE%B5/Selenium%E8%87%AA%E5%8A%A8%E5%8C%96/selenium_demo/day07demo/exercise01.html')
        #元素定位
        self.myUsername=self.driver.find_element(By.ID,'username')
        self.myMobile=self.driver.find_element(By.ID,'mobile')
        self.myMale=self.driver.find_element(By.ID,'male')
        self.myFmale = self.driver.find_element(By.ID, 'female')
        self.myWechat=self.driver.find_element(By.XPATH,'//*[@id="wechat"]')
        self.mysubmit = self.driver.find_element(By.ID, "submit")
        self.myreset = self.driver.find_element(By.ID, "reset")
        #操作元素
        self.myUsername.send_keys(self.data1["username"])
        sleep(2)
        self.myFmale.click()
        sleep(2)
        self.myMobile.send_keys(self.data1["mobile"])
        sleep(2)
        self.myWechat.send_keys(self.data1["wechat"])
        sleep(2)
        self.mysubmit.click()
        #处理弹出对话框
        alert1=self.driver.switch_to.alert
        r_actual=alert1.text
        sleep(2)
        alert1.accept()
        #通过断言判断是否执行成功
        r_except='提交成功!'
        self.assertEqual(r_except,r_actual)
    def tearDown(self):#不应该放在中间吗,运行顺序
        print('测试环境的撤销')
        self.driver.quit()
#5、执行测试用例
if __name__=='__main__':
    unittest.main()


